// pages/details/details.js
const app = getApp()
var detailsData = require("../inviteHomeDetailsdata/inviteHomeDetailsdata.js");
var commodityBtn=true;//展开收起
Page({

  /**
   * 页面的初始数据
   */
  data: {
    quantity1: {
      quantity: 1,
      min: 1,
      max: 20,
      delStatus: 'disabled',
      addStatus: 'normal'
    },
    isLike: true,
    showDialog: false, 
    imgUrls:'',//轮播图
    current: 0,
    swiperCurrent: 0,
    discountBtn:true,//是否有优惠券
    popupBtn:"true",//弹出框
    specification:"",//产品规格,
    commodityDetails:"",//商品详情规格
    commodityBtn:true,//展开收起
    commodityDetailsImg:"",//产品详情图片
    milkingArr:"",//送奶选择天数
    milkingSelectDay:"",
    milkingTimer:"",//送奶时间
    milkingTimerText:"",
    shoppingcart:true,
    noBuyBtn: false,//不可购买开关
    date: "2018年11月14日",//20200213日期开始默认
    years: "2018",
    months: '11',
    days:"14"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      //推荐消息， 订奶列表数据
      imgUrls: detailsData.imgUrls,
      specification: detailsData.specification,
      commodityDetails:detailsData.commodityDetails,
      commodityDetailsImg: detailsData.commodityDetailsImg,
      milkingArr:detailsData.milkingArr,
      milkingTimer: detailsData.milkingTimer,
      milkingTimerText: detailsData.milkingTimer[0],
      milkingSelectDay: detailsData.milkingArr[0].milkingDay,
    })
  },
  //轮播图的切换事件 
  swiperChange: function (e) {
    //只要把切换后当前的index传给<swiper>组件的current属性即可 
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  //点击指示点切换 
  chuangEvent: function (e) {
    this.setData({
      swiperCurrent: e.currentTarget.id
    })
  },
  shoppingcart: function () {//加入购物车
    this.setData({
      shoppingcart: false
    })
    this.clickme2();
    this.noStock();
  },
  shoppingcartBtn:function(){//加入购物车确定按钮
    var count = this.data.specification;
    count.shoppingcartText++;
    if (count.stock<=0){
      count.stock ==0;
    }else{
      count.stock = count.stock - (count.num * this.data.milkingSelectDay);
      wx.showToast({
        title: '已加入购物车',
        icon: 'success',
        duration: 500
      })
    }
    this.setData({
      specification: count
    })
    this.hideModal();
  },
  orderBtn: function () {//订购
    this.setData({
      shoppingcart: true
    })
    this.clickme2()
    this.noStock();
  },
  noStock:function(){//无库存
    var count = this.data.specification;
    var g = (count.num * this.data.milkingSelectDay)
    if (g>count.stock) {
      this.setData({
        noBuyBtn: true
      })
    }else{
      this.setData({
        noBuyBtn: false
      })
    }
  },
  //点击我显示购买弹出框
  clickme2: function () {
    this.setData({
      popupBtn: false
    })
      this.showModal();

  },
  //显示对话框
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  commodityBtn:function(){
    commodityBtn = !commodityBtn
    this.setData({
      commodityBtn :commodityBtn
    })
  },
  radiochange: function (e) {//选择天数
    console.log('radio发生change事件，携带的value值为：', e.detail.value)
    this.setData({
      milkingSelectDay: e.detail.value
    })
    this.countSum();
    this.noStock();
  },
  add: function (e) {
    // 加
    var count = this.data.specification;
    if (count.num < count.stock ){
      count.num++
    } else {
      count.num==count.stock;
    }
    count.priceSum = (count.num * count.price).toFixed(2);
    this.setData({
      specification: count
    });
    this.countSum();
    this.noStock();
  },
  minus: function (e) {
    // 减
    var count = this.data.specification;
    if (count.num <= 1) {
      count.num = 1
    } else {
      count.num--;
    }
    count.priceSum = (count.num * count.price).toFixed(2);
    this.setData({
      specification: count
    })
    this.countSum();
    this.noStock();
  },
  countSum: function () {
    // 总价
    var count = this.data.specification;
    var milkingSelectDay = this.data.milkingSelectDay;
    count.priceSumday = (count.priceSum * milkingSelectDay).toFixed(2);
    this.setData({
      specification: count
    })
  }, 
  milkingTimer: function (e) {//选择送奶时间
    console.log('radio发生change事件，携带的value值为：', e.detail.value)
    this.setData({
      milkingTimerText: e.detail.value
    })
  },
  goShoppingCard: function () {//跳转到到购物车结算
      wx.redirectTo({
        url: '../inviteHomeshoppingCart/inviteHomeshoppingCart'
      })
  },
  goIndex: function () {//跳转到首页
    wx.redirectTo({
      url: '../inviteHome/inviteHome'
    })
  },
  goCashCoupon: function () {//跳转到代金券页面
    wx.navigateTo({
      url: '../../../../pages/cashCoupon/cashCoupon'
    })
  },
  goZhiFu: function () {//跳转到支付
    wx.navigateTo({
      url: '../../../pages/pay/pay'
    })
  },
  getDateTime: function (e) {//20200213获取日期
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value,
      years: e.detail.value.substring(0, 4),
      months: e.detail.value.substring(5, 7),
      days: e.detail.value.substring(8, 11)
    })
  },
  onShareAppMessage: function (res) {//20200313转发
    return {
      title: '转发',
      path: '/pages/inviteHomeDetails/inviteHomeDetails'
      // imageUrl: '../images/index/one.png'

    }
  },

})